package com.demo.my.Controller;

import com.demo.my.Entity.Department;
import com.demo.my.Entity.Employee;
import com.demo.my.Service.DepartmentService;
import com.demo.my.Service.EmployeeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/DepPreUpdateServlet")
public class DepPreUpdateServlet extends HttpServlet {
    private DepartmentService departmentService=new DepartmentService();
    private EmployeeService employeeService=new EmployeeService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String id = req.getParameter("id");
            Department dep = departmentService.getDepById(id);
            Integer departmentNumber = dep.getDepartmentNumber();
            List<Employee> employees = employeeService.getEmployeeListByDid(departmentNumber);
            req.setAttribute("employees",employees);
            req.setAttribute("department",dep);
            if (dep.getManager()!=null){
            req.setAttribute("deid",Integer.parseInt(dep.getManager()));}
            req.getRequestDispatcher("jsp/admin/department_update.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
